import createjs from 'createjs-ts'
const Point = createjs.Point;
function getRandomHex(range){
	return new Number(Math.round(Math.random()*range)).toString(16);
}
export const getRandomColor = () => {
	let R = getRandomHex(255),
		G = getRandomHex(255),
		B = getRandomHex(255);
	let color = "#" + R + G + B
	if(color.length !== 7) {
		color = getRandomColor();
	}
	return color;
}
export default {
	resizeCanvas: function(canvas){
		var w = window.innerWidth,
			h = window.innerHeight;
		canvas.width = w;
		canvas.height = h;
		return [w, h]
	},
	getRandomColor
}

export const getUniqueID = () => {
	return Math.random().toString().substring(2,18)
}

export const getRandomMoveDirection = () => {
	let moveDirection  = Math.random() * Math.PI * 2; // 初始跑动方向
	return new Point(Math.cos(moveDirection), Math.sin(moveDirection))
}

/**
 * 计算缩放比例
 * @param {*} context 
 * @returns 
 */
export const getPixelRatio = (context) => {
	const backingStore = context.backingStorePixelRatio
	|| context.webkitBackingStorePixelRatio
	|| context.mozBackingStorePixelRatio
	|| context.msBackingStorePixelRatio
	|| context.oBackingStorePixelRatio
	|| context.backingStorePixelRatio
	|| 1;
	return (window.devicePixelRatio || 1) / backingStore;
};

/**
 * 获取运行环境
 * @returns 'pc' |'mobile'
 */
export function browserRedirect() {
	var sUserAgent = navigator.userAgent.toLowerCase();
	var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
	var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
	var bIsMidp = sUserAgent.match(/midp/i) == "midp";
	var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
	var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
	var bIsAndroid = sUserAgent.match(/android/i) == "android";
	var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
	var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
	if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {
		return "mobile";
	} else {
		return "pc";
	}
}